﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;

///* OpenXml */
using System.IO;
//using System.Xml;
//using OfficeOpenXml;
//using System.Drawing;
//using OfficeOpenXml.Style;
///* OpenXml */

using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.DAL;
using KPIS.GERP.GFMS.MODEL;

using KPIS.GERP.GFMS.APP.App_UI.Commons;


namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class FIM_ChequesNew : System.Web.UI.Page
    {
        Main main = new Main();
        IBLL_FIM_Cheques cheque = new B_FIM_Cheques();
        IBLL_FIM_ChequeBooks cb = new B_FIM_ChequeBooks();
        IBLL_FIM_MAS_Banks bank = new B_FIM_MAS_Banks();
        IBLL_FIM_MAS_BankBranchs branch = new B_FIM_MAS_BankBranchs();
        IBLL_FIM_MAS_BankAccounts acc = new B_FIM_MAS_BankAccounts();
        IBLL_BGM_MAS_Source source = new B_BGM_MAS_Source();
        IBLL_FIM_Payments pay = new B_FIM_Payments();
        IBLL_FIM_PaymentCheques pay_cheque = new B_FIM_PaymentCheques();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //======= test only =========
                //Session["EmpSeq"] = "1";
                //===========================
                lblChequeSeq.Text = Request.QueryString["cheque_seq"];

                LoadDDLBankAccSeq();
                ddlBankAccSeq.SelectedIndex = 0;
                UpdateUI1("ViewAll");
                UpdateUI2("ViewAll");
                LoadRadGrid2("-1");
                RadGrid2.Enabled = false;
                PanelControl2.Enabled = false;
            }
        }

        protected void LoadRadGrid1()
        {
            RadGrid1.DataSource = cb.GetFull(ddlBankAccSeq.SelectedValue, null);
        }

        protected void LoadRadGrid2(string strChequeBookSeq)
        {
            RadGrid2.DataSource = cheque.GetFull(null, strChequeBookSeq, null);
            RadGrid2.DataBind();
        }

        protected void LoadDDLBankAccSeq()
        {
            ddlBankAccSeq.DataSource = acc.GetList("2", null, null, null);
            ddlBankAccSeq.DataBind();
        }

        protected void InsertChequeInChequeBook(string strChequeBookSeq
            , string strStartChequeNo, string strEndChequeNo)
        {
            if (strStartChequeNo.Length > 0 && strEndChequeNo.Length > 0)
            {
                Nullable<int> nChequeBookSeq = main.ConvertNullInt(strChequeBookSeq);
                Nullable<int> nBankAccSeq = main.ConvertNullInt(ddlBankAccSeq.SelectedValue);
                Nullable<int> nStartChequeNo = main.ConvertNullInt(strStartChequeNo);
                Nullable<int> nEndChequeNo = main.ConvertNullInt(strEndChequeNo);
                Nullable<int> nUserID = main.ConvertNullInt(Session["EmpSeq"]);
                Nullable<DateTime> dtNow = main.ConvertNullDateTime(DateTime.Now);

                for (Nullable<int> nChequeNo = nStartChequeNo; nChequeNo <= nEndChequeNo; nChequeNo++)
                {
                    M_FIM_Cheques info = new M_FIM_Cheques();
                    info.cheque_book_seq = nChequeBookSeq;
                    info.cheque_no = nChequeNo;
                    info.bank_acc_seq = nBankAccSeq;
                    info.cheque_status = "1";
                    info.created_by = nUserID;
                    info.created_when = dtNow;

                    cheque.Insert(info);
                }
            }

            LoadRadGrid2(strChequeBookSeq);
            RadGrid2.DataBind();
        }

        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "ViewAll":
                case "Insert":
                case "Update":
                    cmdViewAll1.Visible = false;
                    cmdAddNew1.Visible = true;
                    cmdEdit1.Visible = true;
                    cmdDelete1.Visible = true;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = false;

                    PanelRadGrid1.Visible = true;
                    PanelEdit1.Visible = false;
                    break;
                case "AddNew":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = true;
                    cmdUpdate1.Visible = false;

                    PanelRadGrid1.Visible = false;
                    PanelEdit1.Visible = true;
                    txtStartChequeNo.ReadOnly = false;
                    txtEndChequeNo.ReadOnly = false;
                    break;
                case "Edit":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = true;

                    PanelRadGrid1.Visible = false;
                    PanelEdit1.Visible = true;
                    txtStartChequeNo.ReadOnly = true;
                    txtEndChequeNo.ReadOnly = true;
                    break;
            }
        }

        protected void UpdateUI2(string strCommand)
        {
            switch (strCommand)
            {
                case "ViewAll":
                case "Insert":
                case "Update":
                    cmdViewAll2.Visible = false;
                    //cmdAddNew2.Visible = true;
                    cmdEdit2.Visible = true;
                    cmdDelete2.Visible = true;
                    cmdInsert2.Visible = false;
                    cmdUpdate2.Visible = false;

                    PanelRadGrid2.Visible = true;
                    PanelEdit2.Visible = false;
                    PanelControl1.Enabled = true;
                    PanelRadGrid1.Enabled = true;
                    break;
                case "AddNew":
                    cmdViewAll2.Visible = true;
                    //cmdAddNew2.Visible = false;
                    cmdEdit2.Visible = false;
                    cmdDelete2.Visible = false;
                    cmdInsert2.Visible = true;
                    cmdUpdate2.Visible = false;

                    PanelRadGrid2.Visible = false;
                    PanelEdit2.Visible = true;
                    PanelControl1.Enabled = false;
                    PanelRadGrid1.Enabled = false;
                    break;
                case "Edit":
                    cmdViewAll2.Visible = true;
                    //cmdAddNew2.Visible = false;
                    cmdEdit2.Visible = false;
                    cmdDelete2.Visible = false;
                    cmdInsert2.Visible = false;
                    cmdUpdate2.Visible = true;

                    PanelRadGrid2.Visible = false;
                    PanelEdit2.Visible = true;
                    PanelControl1.Enabled = false;
                    PanelRadGrid1.Enabled = false;
                    break;
            }
        }

        protected void ClearControl1()
        {
            txtChequeBookNo.Text = "";
            txtStartChequeNo.Text = "";
            txtEndChequeNo.Text = "";
            txtChequeBookRemarks.Text = "";
        }

        protected void ClearControl2()
        {
            txtChequeNo.Text = "";
            txtReceiver.Text = "";
            dpChequeDate.SetValue(null);
            dpChequePayDate.SetValue(null);
            txtActualPayDate.SetValue(null);
            txtChequeRemarks.Text = "";
        }

        //============================ control events ==============================

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "RowClick" && e.Item is GridDataItem)
            {
                e.Item.Selected = true;
                string strChequeBookSeq = e.Item.OwnerTableView.Items[e.Item.ItemIndex]["cheque_book_seq"].Text;
                string strChequeBookNo = e.Item.OwnerTableView.Items[e.Item.ItemIndex]["cheque_book_no"].Text;
                //Session["BankAccSeq"] = strBankAccSeq;
                RadGrid2.Enabled = true;
                PanelControl2.Enabled = true;
                LoadRadGrid2(strChequeBookSeq);
                lblChequeBookSeq.Text = strChequeBookSeq;
                lblChequeBookNo.Text = strChequeBookNo;
            }
        }

        protected void RadGrid2_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Sort" || e.CommandName == "Filter")
            {
                LoadRadGrid2(RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["cheque_book_seq"].Text);
            }
           
            //else if (e.CommandName == "History")
            //{
            //    RadWindow1.Visible = true;
            //    RadWindow1.Controls.Add(new  FIM_Mas_BankAccountHistoriess());
            //}
        }

        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                LoadRadGrid1();
            }
        }

        protected void cmdViewAll1_Click(object sender, EventArgs e)
        {
            UpdateUI1("ViewAll");

            ClearControl1();
        }

        protected void cmdAddNew1_Click(object sender, EventArgs e)
        {
            UpdateUI1("AddNew");
            lblErrormsg.Visible = false;
            //string strBankName = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["bank_name"].Text;
            //string strBranchName = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["branch_name"].Text;

            //lblBankName.Text = strBankName;
            //lblBranchName.Text = strBranchName;
        }

        protected void cmdEdit1_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                UpdateUI1("Edit");

                string strChequeBookSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["cheque_book_seq"].Text;
                string strChequeBookNo = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["cheque_book_no"].Text;
                string strBankAccSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["bank_acc_seq"].Text;
                string strStartChequeNo = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["start_cheque_no"].Text;
                string strEndChequeNo = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["end_cheque_no"].Text;
                string strChequeBookStatus = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["cheque_book_status"].Text;
                string strChequeBookRemarks = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["remarks"].Text;

                lblChequeBookSeq.Text = main.WebString(strChequeBookSeq);
                txtChequeBookNo.Text = main.WebString(strChequeBookNo);
                //lblBankAccSeq.Text = main.WebString(strBankAccSeq);
                txtStartChequeNo.Text = main.WebString(strStartChequeNo);
                txtEndChequeNo.Text = main.WebString(strEndChequeNo);
                txtChequeBookRemarks.Text = main.WebString(strChequeBookRemarks);
                rblChequeBookStatus.SelectedValue = main.WebString(strChequeBookStatus);
            }
            else
            {
            }
        }

        protected void cmdDelete1_Click(object sender, EventArgs e)
        {
            M_FIM_ChequeBooks info = new M_FIM_ChequeBooks();
            string strPK = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["cheque_book_seq"].Text;
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            info.cheque_book_seq = Convert.ToInt32(strPK);
            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = main.ConvertNullDateTime(dtNow);

            cb.Delete(info);
            LoadRadGrid1();
            RadGrid1.DataBind();
        }

        protected void cmdInsert1_Click(object sender, EventArgs e)
        {
            M_FIM_ChequeBooks info = new M_FIM_ChequeBooks();
            string strChequeBookSeq = null;
            string strBankAccSeq = ddlBankAccSeq.SelectedValue;
            string strChequeBookNo = txtChequeBookNo.Text;
            string strStartChequeNo = txtStartChequeNo.Text;
            string strEndChequeNo = txtEndChequeNo.Text;
            string strRemarks = txtChequeBookRemarks.Text;
            string strChequeBookStatus = rblChequeBookStatus.SelectedValue;
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            info.bank_acc_seq = main.ConvertNullInt(strBankAccSeq);
            info.cheque_book_no = strChequeBookNo;
            info.start_cheque_no = main.ConvertNullInt(strStartChequeNo);
            info.end_cheque_no = main.ConvertNullInt(strEndChequeNo);
            info.remarks = strRemarks;
            info.cheque_book_status = strChequeBookStatus;

            info.created_by = main.ConvertNullInt(strUserID);
            info.created_when = main.ConvertNullDateTime(dtNow);

            //cb.SelectRecentInsertedRoleSeq();

            int RunCheque;
            int strChequeNStart = Convert.ToInt32(txtStartChequeNo.Text);
            int strChequeEnd = Convert.ToInt32(txtEndChequeNo.Text);
            RunCheque = strChequeNStart + 100;
            if (strChequeEnd > RunCheque)
            {
                lblErrormsg.Visible = true;
                lblErrormsg.Text = "กรุณาตรจสอบเลขที่เช็คอีกครั้ง";
                ClearControl1();
            }
            else
            {
                strChequeBookSeq = cb.Insert(info);
                InsertChequeInChequeBook(strChequeBookSeq, strStartChequeNo, strEndChequeNo);
                LoadRadGrid1();
                RadGrid1.DataBind();
                UpdateUI1("Insert");
                ClearControl1();
            }
        }

        protected void cmdUpdate1_Click(object sender, EventArgs e)
        {
            M_FIM_ChequeBooks info = new M_FIM_ChequeBooks();
            string strChequeBookSeq = lblChequeBookSeq.Text;
            string strBankAccSeq = ddlBankAccSeq.SelectedValue;
            string strChequeBookNo = txtChequeBookNo.Text;
            string strStartChequeNo = txtStartChequeNo.Text;
            string strEndChequeNo = txtEndChequeNo.Text;
            string strRemarks = txtChequeBookRemarks.Text;
            string strChequeBookStatus = rblChequeBookStatus.SelectedValue;
            string strUserID = Session["EmpSeq"].ToString();
             
            DateTime dtNow = DateTime.Now;

            info.cheque_book_seq = Convert.ToInt32(strChequeBookSeq);
            info.bank_acc_seq = main.ConvertNullInt(strBankAccSeq);
            info.cheque_book_no = strChequeBookNo;
            info.start_cheque_no = main.ConvertNullInt(strStartChequeNo);
            info.end_cheque_no = main.ConvertNullInt(strEndChequeNo);
            info.remarks = strRemarks;
            info.cheque_book_status = strChequeBookStatus;

            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = main.ConvertNullDateTime(dtNow);

            cb.Update(info);

            LoadRadGrid1();
            RadGrid1.DataBind();
            UpdateUI1("Update");
            ClearControl1();
        }

        protected void cmdViewAll2_Click(object sender, EventArgs e)
        {
            UpdateUI2("ViewAll");

            ClearControl2();
        }
            

        //protected void cmdAddNew2_Click(object sender, EventArgs e)
        //{
        //    UpdateUI2("AddNew");

        //    //string strBankName = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["bank_name"].Text;
        //    //string strBranchName = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["branch_name"].Text;

        //    //lblBankName.Text = strBankName;
        //    //lblBranchName.Text = strBranchName;
        //    dpPayDate.SetValue(DateTime.Today);
        //}

        protected void cmdEdit2_Click(object sender, EventArgs e)
        {
            if (RadGrid2.SelectedItems.Count > 0)
            {
                UpdateUI2("Edit");

                string strChequeSeq = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["cheque_seq"].Text;
                string strChequeNo = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["cheque_no"].Text;
                string strBankAccSeq = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["bank_acc_seq"].Text;
                string strPayDate = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["pay_date"].Text;
                string strActualPayDate = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["actual_pay_date"].Text;
                string strReceiver = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["receiver"].Text;
                string strRemarks = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["remarks"].Text;
                string strChequeStatus = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["cheque_status"].Text;
                string strChequeDate = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["cheque_date"].Text;

                //string strBankName = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["bank_name"].Text;
                //string strBranchName = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["branch_name"].Text;

                lblChequeSeq.Text = main.WebString(strChequeSeq);
                txtChequeNo.Text = main.WebString(strChequeNo);
                lblBankAccSeq.Text = main.WebString(strBankAccSeq);
                dpChequePayDate.SetValue(main.WebString(strPayDate));
                dpChequeDate.SetValue(main.WebString(strChequeDate));
                txtActualPayDate.SetValue(main.WebString(strActualPayDate));
                txtReceiver.Text = main.WebString(strReceiver);
                rblChequeStatus.SelectedValue = main.WebString(strChequeStatus);
                txtChequeRemarks.Text = main.WebString(strRemarks);

                rblChequeStatus_SelectedIndexChanged(null,null);
                //lblBankName.Text = strBankName;
                //lblBranchName.Text = strBranchName;
            }
            else
            {
            }
        }

        protected void cmdDelete2_Click(object sender, EventArgs e)
        {
            M_FIM_Cheques info = new M_FIM_Cheques();
            string strPK = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["cheque_seq"].Text;
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            info.cheque_seq = Convert.ToInt32(strPK);
            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = main.ConvertNullDateTime(dtNow);

            cheque.Delete(info);
            LoadRadGrid2(RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["cheque_book_seq"].Text);
        }

        protected void cmdInsert2_Click(object sender, EventArgs e)
        {
            M_FIM_Cheques info = new M_FIM_Cheques();
            string strChequeBookSeq = lblChequeBookSeq.Text;
            string strBankAccSeq = lblBankAccSeq.Text;
            string strChequeNo = txtChequeNo.Text;
            string strPaydate = dpChequePayDate.GetStringValue();
            string strChequedate = dpChequeDate.GetStringValue();
            string strReceiver = txtReceiver.Text;
            string strChequeStatus = rblChequeStatus.SelectedValue;
            string strRemarks = txtChequeRemarks.Text;
            string strUserID = Session["EmpSeq"].ToString();
            Nullable<DateTime> dtActualPayDate = txtActualPayDate.GetDateValue();
            DateTime dtNow = DateTime.Now;

            if (strChequeStatus == "3" && dtActualPayDate == null)
            {
                lblErrorActualPayDate.Visible = true;
                return;
            }
            else
            {
                dtActualPayDate = null;
            }

            lblErrorActualPayDate.Visible = false;

            info.cheque_book_seq = main.ConvertNullInt(strChequeBookSeq);
            info.bank_acc_seq = main.ConvertNullInt(strBankAccSeq);
            info.cheque_no = main.ConvertNullInt(strChequeNo);
            info.pay_date = main.ConvertNullDateTime(strPaydate);
            info.cheque_date = main.ConvertNullDateTime(strChequedate);
            info.actual_pay_date = dtActualPayDate;
            info.receiver = strReceiver;
            info.remarks = strRemarks;
            info.cheque_status = strChequeStatus;
            info.actual_pay_date = dtActualPayDate;
            info.created_by = main.ConvertNullInt(strUserID);
            info.created_when = main.ConvertNullDateTime(dtNow);

            cheque.Insert(info);

            LoadRadGrid2(strChequeBookSeq);
            UpdateUI2("Insert");
            ClearControl2();
        }

        protected void cmdUpdate2_Click(object sender, EventArgs e)
        {
            M_FIM_Cheques info = new M_FIM_Cheques();
            string strChequeBookSeq = lblChequeBookSeq.Text;
            string strChequeSeq = lblChequeSeq.Text;
            string strBankAccSeq = lblBankAccSeq.Text;
            string strChequeNo = txtChequeNo.Text;
            string strPaydate = dpChequePayDate.GetStringValue();
            string strChequedate = dpChequeDate.GetStringValue();
            string strReceiver = txtReceiver.Text;
            string strRemarks = txtChequeRemarks.Text;
            string strChequeStatus = rblChequeStatus.SelectedValue;
            string strUserID = Session["EmpSeq"].ToString();
            Nullable<DateTime> dtActualPayDate = txtActualPayDate.GetDateValue();
            DateTime dtNow = DateTime.Now;

            if (strChequeStatus == "3" && dtActualPayDate == null)
            {
                lblErrorActualPayDate.Visible = true;
                return;
            }

            lblErrorActualPayDate.Visible = false;

            info.cheque_seq = Convert.ToInt32(strChequeSeq);
            info.cheque_book_seq = main.ConvertNullInt(strChequeBookSeq);
            info.bank_acc_seq = main.ConvertNullInt(strBankAccSeq);
            info.cheque_no = main.ConvertNullInt(strChequeNo);
            info.pay_date = main.ConvertNullDateTime(strPaydate);
            info.cheque_date = main.ConvertNullDateTime(strChequedate);
            info.actual_pay_date = dtActualPayDate;
            info.receiver = strReceiver;
            info.remarks = strRemarks;
            info.cheque_status = strChequeStatus;
            info.actual_pay_date = dtActualPayDate;
            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = main.ConvertNullDateTime(dtNow);

            cheque.Update(info);

            LoadRadGrid2(strChequeBookSeq);
            UpdateUI2("Update");
            ClearControl2();
        }

        protected void ddlBankAccSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadRadGrid1();
            RadGrid1.DataBind();
            LoadRadGrid2("-1");
        }

        protected void RadGrid2_PageIndexChanged(object source, GridPageChangedEventArgs e)
        {
            LoadRadGrid2(RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["cheque_book_seq"].Text);
        }

        protected void rblChequeStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            liActualPayDate.Visible = rblChequeStatus.SelectedValue == "3";
        }

        protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem dataBoundItem = e.Item as GridDataItem;

                var strStatus = dataBoundItem["cheque_status"].Text;
                GridDataItem cmditem = (GridDataItem)e.Item;
                LinkButton lbtnadd = (LinkButton)cmditem.FindControl("cmdPrintChequePopup");
                if (strStatus.Equals("1"))
                    lbtnadd.Visible = true;
                else
                    lbtnadd.Visible = false;
              
            }
        }

        //==========================================================================
    }
}
